/*
	序列号详情
		序列号详情
		序列号信息修改
*/
//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var DICT_AGT = JSON.parse(sessionStorage.getItem('kplus_web_dict_agt'));
var DICT_ADMIN = JSON.parse(sessionStorage.getItem('kplus_web_dict_admin'));
var DICT_PNAME  = JSON.parse(sessionStorage.getItem('kplus_web_dict_pName'));

//dom
$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer,
			form = layui.form;
		//ajax data	
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken : adminLoginedInfo.aToken
		};
		//存储psn信息
		var storage_psn_info = {};
		//添加字典
		createDicts(DICT_AGT, DICT_PNAME);
		form.render();
		//获取序列号信息
		function getPsn(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({code: getParam('psn')}, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.psn,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						createFormData(responseData.data.data[0]);
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						//跳转500
						location.href = './500.html';
					}});
				}
			})
		};
		getPsn(ajaxData)
		//构建表单数据
		function createFormData(formData){
			//console.log(formData);
			var init_form_data = {
				code: formData.code,
				belong: formData.belong,
				agtCode: formData.agent,
				size: formData.size,
				admin: DICT_ADMIN[formData.create_adm_id],
				createTime: formatTime(formData.format_time),
				useTime: formData.used_time,
				actTime: formData.active_time,
				salesman: formData.salesman,
				status: formData.status
			};
			storage_psn_info = init_form_data;
			form.val('psn-form', init_form_data);
			form.render(); 
		};

		//确认修改
		form.on('submit(modify)', function(data){
			//console.log(data);
			layer.confirm(MESSAGES.confirm_modify_psn, {title:'提示'}, function(index){
				modifyPsn(data.field);
				layer.close(index); //关闭询问框
			});
			return false;
		})
		// 修改序列号信息
		function modifyPsn(psnData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			//对比存储变换
			var modifyData = isObjectValueEqual(storage_psn_info ,psnData);
			if(Object.keys(modifyData).length){   //存在变更 key键转化
				if(modifyData['salesman']) modifyData['man'] = modifyData['salesman'];
				if(modifyData['useTime']) modifyData['time'] = modifyData['useTime'];
				var _ajaxData = $.extend({'code': getParam('psn')}, modifyData, ajaxData);
				//console.log(_ajaxData)
				$.ajax({
					url: BASE_SERVICEURL + URlS.psn,
					type: 'POST', 
					data: _ajaxData,
					success: function(responseData){
						if(responseData.msg.code == 200){
							layer.close(loadIndex);
							layer.msg(MESSAGES.modify_info_success, {time:2000, end: function(){
								location.reload();
							}});
						}else{
							layer.close(loadIndex);
							layer.msg(responseData.msg.msg, {time: 2000});
						}
					},
					error: function(responseData){
						layer.close(loadIndex);
						layer.msg(MESSAGES.service_error, {time: 2000});
					}
				})
			}
			
		}



	});
})







